package com.mocchira.music.radio;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class DownloadManager {
    private Context context;
    private IDownloadListener listener;
    public DownloadManager(Context ctx, IDownloadListener listener) {
        this.context = ctx;
        this.listener = listener;
    }
    protected class DownloadTask extends AsyncTask<String, Integer, String> {
        protected String url;
        protected File tmpFile;
        protected Exception exception = null;
        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient client = null;
            InputStream in = null;
            FileOutputStream out = null;
            try {
                url = params[0];
                tmpFile = File.createTempFile(this.toString(), ".tmp", DownloadManager.this.context.getCacheDir());
                client = new DefaultHttpClient();
                HttpGet method = new HttpGet(url);
                method.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13");
                HttpResponse res = client.execute(method);
                in = res.getEntity().getContent();
                out = new FileOutputStream(tmpFile);
                int len = 0;                       
                byte[] buf = new byte[8192];       
                while ((len = in.read(buf)) != -1){
                    out.write(buf, 0, len);
                }
            } catch (Exception ex) {
                exception = ex;
                if (ex instanceof RuntimeException)
                    System.gc(); // for OutofMemory
                Log.e(this.getClass().getName(), ex.getMessage(), ex);
            } finally {
                try {
                    if (in != null) in.close();
                    if (out != null) out.close();
                    if (client != null) client.getConnectionManager().shutdown();
                } catch (Exception ex) {
                    ;
                }
            }
            return url;
        }
        @Override
        protected void onPostExecute(String url) {
            try {
                DownloadManager.this.listener.onComplete(url, tmpFile, exception);
            } catch (Exception ex) {
                Log.e(this.getClass().getName(), ex.getMessage(), ex);
            }
        }
    }

    public void enqueue(String url) {
        DownloadTask task = new DownloadTask();
        task.execute(url);
    }
    
    //sync
    public void execute(String url) {
        DownloadTask task = new DownloadTask();
        String[] params = new String[1];
        params[0] = url;
        task.doInBackground(params);
        listener.onComplete(url, task.tmpFile, task.exception);
    }
}
